<template>
  <div class="todoapp">
    <myHeader @addList="addFn" :list="list"></myHeader>
    <myMain :myList="showArr" @delList="delFn"></myMain>
    <myFooter :counts="showArr" @myShow="showFn" @myClear="clearBtn"></myFooter>
  </div>
</template>

<script>
import myHeader from "./components/header";
import myMain from "./components/main";
import myFooter from "./components/footer";
export default {
  components: {
    myHeader,
    myMain,
    myFooter,
  },
  data() {
    return {
      isArr: "all",
      list: JSON.parse(localStorage.getItem("newList")) || [],
    };
  },
  methods: {
    addFn(name) {
      this.list.push({
        id: this.length === 0 ? 100 : this.list[this.list.length - 1].id + 1,
        name,
        isDone: false,
      });
    },
    delFn(index) {
      this.list.splice(index, 1);
    },
    showFn(str) {
      this.isArr = str;
    },
    clearBtn() {
      this.list = this.list.filter((obj) => obj.isDone === false);
    },
  },
  computed: {
    showArr() {
      if (this.isArr === "yes") {
        return this.list.filter((obj) => obj.isDone === true);
      } else if (this.isArr === "no") {
        return this.list.filter((obj) => !obj.isDone === true);
      } else {
        return this.list;
      }
    },
  },
  watch: {
    list: {
      handler(newVal) {
        localStorage.setItem("newList", JSON.stringify(newVal));
      },
      deep: true,
    },
  },
};
</script>

<style>
</style>